package com.lqm.dao;


import com.lqm.entity.home.HomeUser;
import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface HomeUserDao  extends JpaRepository<HomeUser,Integer>, PagingAndSortingRepository<HomeUser,Integer> {


    @Query(value = "select h from HomeUser h " )
    List<HomeUser> findHomeUser();

    @Modifying
    @Query(value = "update HomeUser h set h.status=:status where h.id= :#{#homeUser.id}")
    void setHomeUserStatus(HomeUser homeUser, int status);

    /**
     * 通过用户名密码查询用户，用户登录
     * @param u 用户名
     * @param p 密码
     * @return
     */
    HomeUser findByUsernameAndPassword(String u,String p);

    /**
     * 通过id查询用户
     * @param id
     * @return
     */
    HomeUser findById(int id);

    /**
     * 通过id查询用户
     * @param id
     * @return
     */
    HomeUser getById(int id);


    /**
     * 通过名字查询用户
     * @param name
     * @return
     */
    HomeUser getHomeUserByUsername(String name);

    /**
     * 用户充值
     * @param homeUser
     * @param account
     * @return
     */
    @Modifying
    @Query(value = "update HomeUser h set h.account=h.account+:account where h.id= :#{#homeUser.id}")
    Integer recharge(@Param("homeUser") HomeUser homeUser, @Param("account")Double account);

    /**
     * 用户投资扣钱
     * @param homeUser
     * @param account
     * @return
     */
    @Modifying
    @Query(value = "update HomeUser h set h.account=h.account-:account where h.id= :#{#homeUser.id}")
    Integer divMoney(@Param("homeUser") HomeUser homeUser, @Param("account")Double account);




    @Modifying
    @Query("update HomeUser h set h.password=:newPassword where h.id=:#{#homUser.id}")
    Integer changePassword(HomeUser homeUser,String newPassword);


}
